package com.sensePlusVR.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.NinePatchDrawable;

import java.io.ByteArrayOutputStream;

public class BitmapDrawableUtil {
	
	/**
	 * Drawable --> Bitmap
	 * @param drawable
	 * @return
	 */
	public static Bitmap drawable2Bitmap(Drawable drawable) {  
	        if (drawable instanceof BitmapDrawable) {  
	            return ((BitmapDrawable) drawable).getBitmap();  
	        } else if (drawable instanceof NinePatchDrawable) {  
	            Bitmap bitmap = Bitmap  
	                    .createBitmap(  
	                            drawable.getIntrinsicWidth(),  
	                            drawable.getIntrinsicHeight(),  
	                            drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888  
	                                    : Bitmap.Config.RGB_565);  
	            Canvas canvas = new Canvas(bitmap);  
	            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),  
	                    drawable.getIntrinsicHeight());  
	            drawable.draw(canvas);  
	            return bitmap;  
	        } else {  
	            return null;  
	        }  
	    }  
	
	/**
	 * Bitmap --> Drawable
	 * @param bitmap
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static Drawable bitmap2Drawable(Bitmap bitmap) {  
        return new BitmapDrawable(bitmap);  
    }  
	
	/**
	 * Bitmap --> byte[]
	 * @param bm
	 * @return
	 */
	public static  byte[] Bitmap2Bytes(Bitmap bm) {  
	        ByteArrayOutputStream baos = new ByteArrayOutputStream();  
	        bm.compress(Bitmap.CompressFormat.PNG, 100, baos);  
	        return baos.toByteArray();  
	    }  
	
	/**
	 *  byte[] --> Bitmap
	 * @param b
	 * @return
	 */
	public static Bitmap Bytes2Bimap(byte[] b) {  
	        if (b.length != 0) {  
	            return BitmapFactory.decodeByteArray(b, 0, b.length);  
	        } else {  
	            return null;  
	        }  
	    }  

}
